from bs4 import BeautifulSoup
import requests
url = 'http://www.weather.com.cn/weather/101090101.shtml'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'}
response = requests.get(url=url, headers=headers)
response.encoding=response.apparent_encoding
data=response.text
soup = BeautifulSoup(data, 'lxml')
#根据结构解析数据
lis = soup.select("ul[class= 't clearfix'] li")
print(lis)
with  open('weather.csv','w',encoding='utf-8') as file:
    for li in lis:
        date = li.select('h1')[0].text
        weather = li.select("p[class = 'wea']")[0].text
        tem = li.select("p[class = 'tem'] span")[0].text + "/" + li.select("p[class = 'tem'] i")[0].text
        print(date,weather,tem)
        #保存到文件夹
        file.write(str(date+","+weather+","+tem)+'\n')
